package com.wlh.animation.demo.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.view.NestedScrollingParent;
import android.support.v4.view.NestedScrollingParentHelper;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

/**
 * 吸顶效果
 * Created by weilh on 2018/3/5.
 */

public class CeilingScrollView extends LinearLayout implements NestedScrollingParent{
    NestedScrollingParentHelper mHelper;
    public CeilingScrollView(Context context) {
        super(context);
        init();
    }

    public CeilingScrollView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CeilingScrollView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CeilingScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        mHelper = new NestedScrollingParentHelper(this);
    }

    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
        return true;
    }

    @Override
    public void onStopNestedScroll(View child) {
        mHelper.onStopNestedScroll(child);
    }

    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
        int y = 0;
        if (dy < 500) {
            consumed[1] = dy;
            y = dy;
        }
        scrollBy(dx, y);
    }


    @Override
    public void onNestedScrollAccepted(View child, View target, int axes) {
        mHelper.onNestedScrollAccepted(child, target, axes);
    }

    @Override
    public void scrollTo(int x, int y) {
        super.scrollTo(x, y);
    }

}
